package com.example.rotateimageview.recycleView

import android.graphics.Canvas
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

/**
 * author  : Liushuai
 * time    : 2021/10/17 16:01
 * desc    :
 *
 * 其他的一些方法：
 * startDrg 开启对某个item的拖动 用于定点拖动 比如局部的控件拖动时才能拖动item
 */
class ItemTouchHelper :ItemTouchHelper.Callback() {
    /**
     * 可以滑动的方向
     *  0 不响应
     */
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        //拖拽方向
        var dragFlag = ItemTouchHelper.UP
        val swopeFlag=ItemTouchHelper.LEFT
        return makeMovementFlags(dragFlag,swopeFlag)
    }

    /**
     * 拖拽过程手指移动到 item上方
     */
    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        return true
    }

    /**
     * 将item侧滑出去的时候触发
     */
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        TODO("Not yet implemented")
    }

    /**
     * item 被拖拽或者滑动时状态变化回调
     */
    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
    }

    /**
     * 手指释放后的重置操作
     */
    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
    }

    /**
     * 滑动或者拖拽时会一直调用 可以执行一些变化比如透明度的变化
     */
    override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
        //super通过默认方法 调用translate来移动画布实现移动的 去掉的话将移动不了
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
    }

}